<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
    >
      <el-form-item label="部门名称" prop="department_name">
        <el-input
          v-model="dataForm.department_name"
          placeholder="department_name"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门负责人编号" prop="department_leader_id">
        <el-input
          v-model="dataForm.department_leader_id"
          placeholder="department_leader_id"
        ></el-input>
      </el-form-item>
      <el-form-item label="上级部门编码" prop="superior_department_id">
        <el-input
          v-model="dataForm.superior_department_id"
          placeholder="superior_department_id"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门描述" prop="department_desc">
        <el-input
          v-model="dataForm.department_desc"
          placeholder="department_desc"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        department_name: "",
        department_desc: "",
        department_leader_id: "",
        superior_department_id: "",
      },
      dataRule: {
        department_name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
        ],
        department_desc: [
          { required: true, message: "部门描述不能为空", trigger: "blur" },
        ],
        department_leader_id: [
          {
            required: true,
            message: "部门负责人编号不能为空",
            trigger: "blur",
          },
        ],
        superior_department_id: [
          { required: true, message: "上级部门编号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/business/edepartment/queryDepartment`),
            method: "get",
            params: this.$http.adornParams({
              departmentId: this.dataForm.id,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.department_name =
                data.content.records[0].department_name;
              this.dataForm.department_desc =
                data.content.records[0].department_desc;
              this.dataForm.department_leader_id =
                data.content.records[0].department_leader_id;
              this.dataForm.superior_department_id =
                data.content.records[0].superior_department_id;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/business/edepartment/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              departmentId: this.dataForm.id,
              departmentName: this.dataForm.department_name,
              departmentDesc: this.dataForm.department_desc,
              departmentLeaderId: this.dataForm.department_leader_id,
              superiorDepartmentId: this.dataForm.superior_department_id,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
